package com.woniu.auth.service;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.ExceptionCode;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.token.JwtTokenUtils;
import com.woniu.auth.dto.UserDto;
import com.woniu.auth.dto.WebUserDto;
import com.woniu.auth.feign.UserFeign;
import com.woniu.user.domain.Customer;
import com.woniu.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

@Service
public class LoginService {

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private UserFeign userFeign;

    @Autowired
    private RedisCache redisCache;

    public String login(UserDto userDto) {
        //验证验证码
        String key = UserConstants.CAPTCHA_CODE_KEY+userDto.getUuid();
        String codeInRedis = redisTemplate.opsForValue().get(key);
        String codeInUser = userDto.getCode();
        if (!Objects.equals(codeInRedis, codeInUser)) {
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }

//        String password = customerDto.getPassword();
//        String Md5Password = Md5Utils.hash(Md5Utils.hash(password));

       User user = userFeign.login(userDto.getUsername(), userDto.getPassword());
        if(user == null){
            throw new CustomException(ExceptionCode.INVALID_USERNAME_PASSWORD);
        }

        String token = JwtTokenUtils.generateJwtToken(user.getUserId(),user.getUserName(),user.getUserAccount());
        //保存token到redis
        redisTemplate.opsForValue().set(token,token,30, TimeUnit.MINUTES);
        return token;
    }


    public String webLogin(WebUserDto webUserDto) {
        String key = UserConstants.CAPTCHA_CODE_KEY+webUserDto.getUuid();
        String codeInRedis = redisTemplate.opsForValue().get(key);
        String verifyCode = webUserDto.getVerifyCode();
        if (!Objects.equals(codeInRedis, verifyCode)) {
            throw new CustomException(ExceptionCode.INVALID_KAPTCHA);
        }

        //远程调用user微服务的接口获取用户信息

        Customer customer = userFeign.selectCustomerByAccountAndPass(webUserDto.getCustomerAccount(),
                                                                    webUserDto.getCustomerPassword());
        if (customer == null){
            throw new CustomException(ExceptionCode.INVALID_USERNAME_PASSWORD);
        }

        if (customer.getCustomerStatus() == 2){
            //无效
            throw new CustomException(ExceptionCode.INVALID_ACCOUNT);
        }

        if (customer.getCustomerStatus() == 3){
            //锁定
            throw new CustomException(ExceptionCode.LOCK_ACCOUNT);
        }

        //通过非对称加密生成JWT，并返回,
        String jwt = JwtTokenUtils.generateWebJwtToken(
                customer.getCustomerId(),
                customer.getCustomerName(),
                customer.getCustomerAccount()
        );
        //在返回之前，把jwt保存在redis中，并且超时时间设置为30分钟

        redisTemplate.opsForValue().set(jwt,jwt,30,TimeUnit.DAYS);

        return jwt;
    }
}
